package myArrayList;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/10/14 7:06
 */
public class HomeWork_MyArraylist {
    public static void main(String[] args) {
        HomeWork_MyArraylist homeWork_myArraylist = new HomeWork_MyArraylist();
        homeWork_myArraylist.add(10);
        homeWork_myArraylist.add(20);
        homeWork_myArraylist.add(30);
        homeWork_myArraylist.add(40);
        homeWork_myArraylist.add(50);
        homeWork_myArraylist.add(60);
        homeWork_myArraylist.add(70);
        homeWork_myArraylist.add(80);
        homeWork_myArraylist.add(90);
        homeWork_myArraylist.add(100);
        homeWork_myArraylist.add(110);

        homeWork_myArraylist.display();
        System.out.println(homeWork_myArraylist.isFull());
        homeWork_myArraylist.add(1,15);
        homeWork_myArraylist.add(3,25);
        homeWork_myArraylist.display();
        System.out.println(homeWork_myArraylist.contains(100));
        System.out.println(homeWork_myArraylist.contains(130));
        System.out.println(homeWork_myArraylist.indexOf(70));
        System.out.println(homeWork_myArraylist.get(8));
        homeWork_myArraylist.set(8,68);
        homeWork_myArraylist.display();
        homeWork_myArraylist.remove(68);
        homeWork_myArraylist.display();
        System.out.println(homeWork_myArraylist.size());
        homeWork_myArraylist.clear();
        homeWork_myArraylist.display();
        System.out.println(homeWork_myArraylist.size());
//        System.out.println(homeWork_myArraylist.get(8));
        System.out.println(homeWork_myArraylist.indexOf(70));
        System.out.println(homeWork_myArraylist.contains(130));
        System.out.println(homeWork_myArraylist.isFull());
        System.out.println(homeWork_myArraylist.elem.length);


    }

    public int[] elem;
    public int usedSize;//0
    //默认容量
    private static final int DEFAULT_SIZE = 10;

    public HomeWork_MyArraylist() {
        this.elem = new int[DEFAULT_SIZE];
    }

    /**
     * 打印顺序表:
     * 根据usedSize判断即可
     */
    public void display() {
        if (usedSize == 0 ){
            System.out.print("[ ]");
        }
        for (int i = 0; i < usedSize; i++) {
            if (i == 0) {
                System.out.print("[ ");
            }
            if (i == usedSize - 1) {
                System.out.print(elem[i] + " ]");
                break;
            }
            System.out.print(elem[i] + " , ");
        }
        System.out.println();
    }

    //判断是否需要扩容，如果需要，扩容为2倍
    public void ensureCapacity() {
        if (usedSize < elem.length) {//不需要扩容
            return;
        }
        int[] newElem = new int[elem.length * 2]; //创建新数组
//            System.arraycopy(elem,0,newElem,0,elem.length);
        for (int i = 0; i < this.usedSize; i++) {//搬家
            newElem[i] = elem[i];
        }
        elem = newElem;//改变指向

    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        this.ensureCapacity();
        elem[usedSize] = data;
        usedSize++;
    }

    /**
     * 判断当前的顺序表是不是满的！
     *
     * @return true:满   false代表空
     */
    public boolean isFull() {
        return usedSize == elem.length;
    }


    private boolean checkPosInAdd(int pos) {
        if (pos <0 || pos > usedSize){
            return false;
        }
        return true;//合法
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        this.ensureCapacity();
        if (checkPosInAdd(pos)){
            for (int i = usedSize - 1 ; i >= pos ; i--) {
                elem[i+1] = elem[i];
            }
            elem[pos] = data;
            usedSize++;
        }else {
            return ;//不合法
        }
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
//        for (int i = 0; i < usedSize; i++) {
//            if (elem[i] == toFind){
//                return true;
//            }
//        }
//        return false;
        return indexOf(toFind) != -1;
    }

    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int get(int pos) {
        if (pos<0||pos>= usedSize){
            throw new ArrayIndexOutOfBoundsException("下标不合法");
        }
        return elem[pos];
    }

    private boolean isEmpty() {
        return usedSize == 0;
    }

    // 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, int value) {
        if (pos<0||pos>= usedSize){
            throw new ArrayIndexOutOfBoundsException("下标不合法");
        }
        this.elem[pos] = value;
    }

    /**
     * 删除第一次出现的关键字key
     *
     * @param key
     */
    public void remove(int key) {
        int tem = indexOf(key);
        if (checkPosInAdd(tem)){
            for (int i = tem+1; i < usedSize; i++) {
                elem[i-1] = elem[i];
            }
            usedSize--;
        }
    }

    // 获取顺序表长度
    public int size() {
        return usedSize;
    }

    // 清空顺序表
    public void clear() {
        for (int i = 0; i < usedSize; i++) {
            elem[i] = 0;
        }
        usedSize = 0 ;
    }
}
